"""encoding:utf-8"""
"""提取蘑菇街高跟鞋前100页商品图片"""

import requests,re
from urllib.request import urlretrieve
import time
import os

s = requests.session()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; \
Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0'}

for i in range(1,100):
	time.sleep(3)#降低爬取速度
	url = 'http://list.mogujie.com/search?callback=\
jQuery21107855185055337279_1500688799455&_versio\
n=1&q=%25E9%25AB%2598%25E8%25B7%259F%25E9%259E%25\
8B&cKey=43&minPrice=&_mgjuuid=cff66805-1c53-4953-\
befd-55393476611f&ppath=&page='+str(i)+'&maxPrice=&sort=pop\
&userId=&cpc_offset=&ratio=2%3A3&_=1500688799456'
	os.mkdir('第'+str(i)+'页的图片')#在更换页面后新建文件夹保存图片
	html = s.get(url,headers=headers).text
	links = re.findall(r'"img":"([\s\S]*?)"',html)
	titles = re.findall(r'"title":"([\s\S][^{}]*?)","type',html)
	for link,title in zip(links,titles):
		name = title+link[-4:]
		urlretrieve(link,'./第'+str(i)+'页的图片/'+name)
		#使用链接下载功能下载图片链接并保存在对应创建的文件夹中，
		#并使用商品标题和图片链接后缀作为图片名称
